package com.youlai.boot.modules.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.product.converter.ProductConverter;
import com.youlai.boot.modules.product.mapper.ProductMapper;
import com.youlai.boot.modules.product.model.Product;
import com.youlai.boot.modules.product.model.ProductForm;
import com.youlai.boot.modules.product.model.ProductVO;
import com.youlai.boot.modules.product.model.ProductDetailVO;
import com.youlai.boot.modules.category.model.CategoryVO;
import com.youlai.boot.modules.product.service.ProductService;
import com.youlai.boot.modules.product.model.ProductPageQuery;
import com.youlai.boot.modules.product.model.ProductShippingVO;
import com.youlai.boot.modules.resource.model.ResourceVO;
import com.youlai.boot.modules.contact.model.ContactVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.youlai.boot.modules.resource.mapper.ResourceMapper;
import com.youlai.boot.modules.contact.mapper.ContactMapper;
import com.youlai.boot.modules.delivery.mapper.DeliveryMapper;
import com.youlai.boot.modules.warehouse.mapper.WarehouseMapper;
import com.youlai.boot.modules.country.mapper.CountryMapper;
import com.youlai.boot.modules.country.model.vo.CountryVO;
import com.youlai.boot.common.base.BasePageQuery;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.modules.collect.mapper.CollectMapper;
import com.youlai.boot.modules.collect.model.vo.CollectVO;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    private final ProductConverter productConverter;

    private final ResourceMapper resourceMapper;

    private final ContactMapper contactMapper;

    private final DeliveryMapper deliveryMapper;

    private final WarehouseMapper warehouseMapper;

    private final CountryMapper countryMapper;

    private final CollectMapper collectMapper;

    @Override
    public Page<ProductVO> getProductPage(ProductPageQuery query) {
        Page<ProductVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return baseMapper.getProductPage(page, query);
    }

    @Override
    public ProductDetailVO getProductById(Long productId) {
        // 获取基本信息
        ProductDetailVO productDetail = baseMapper.getProductDetail(productId);
        if (productDetail != null) {
            // 查询商品图片
            List<ResourceVO> images = resourceMapper.getProductImages(productId);
            productDetail.setImages(images);

            // 查询商户联系人
            if (productDetail.getMerchant() != null && productDetail.getMerchant().getId() != null) {
                List<ContactVO> contacts = contactMapper.getMerchantContacts(productDetail.getMerchant().getId());
                productDetail.setContacts(contacts);
            }

            // 查询产品价格图片
            List<ResourceVO> priceImages = resourceMapper.getProductPriceImages(productId);
            productDetail.setPriceImages(priceImages);

            // 查询用户是否收藏此商品
            if (SecurityUtils.getUserId() != null) {
                CollectVO collect = collectMapper.getCollect(SecurityUtils.getUserId(), productId, 1);
                productDetail.setCollect(collect);
            }
        }
        return productDetail;
    }

    @Override
    public ProductShippingVO getProductShipping(Long id) {
        ProductShippingVO shipping = new ProductShippingVO();
        shipping.setId(id);
        shipping.setDeliveryList(deliveryMapper.getProductDeliveries(id));
        shipping.setWarehouseList(warehouseMapper.getProductWarehouses(id));

        // 获取商品国家，使用一个巨大值，确保获取所有国家
        Page<CountryVO> countryPage = new Page<>(1, 1000);
        countryPage = countryMapper.getProductCountryPage(id, countryPage);
        shipping.setCountryList(countryPage.getRecords());
        return shipping;
    }

    @Override
    @Transactional
    public Long saveProduct(ProductForm form) {
        Product product = productConverter.toEntity(form);
        this.save(product);
        Long productId = product.getId();

        if (form.getCountryCodes() != null) {
            baseMapper.saveCountryRelations(productId, form.getCountryCodes());
        }

        return productId;
    }

    @Override
    @Transactional
    public void updateProduct(ProductForm form) {
        Product product = productConverter.toEntity(form);
        this.updateById(product);
        Long productId = form.getId();

        // 更新关联关系
        baseMapper.deleteCategoryRelations(productId);
        baseMapper.deleteCountryRelations(productId);
        baseMapper.deleteDeliveryRelations(productId);
        baseMapper.deleteWarehouseRelations(productId);

        if (form.getCountryCodes() != null) {
            baseMapper.saveCountryRelations(productId, form.getCountryCodes());
        }
    }

    @Override
    @Transactional
    public boolean deleteProduct(Long id) {
        baseMapper.deleteCategoryRelations(id);
        baseMapper.deleteCountryRelations(id);
        baseMapper.deleteDeliveryRelations(id);
        baseMapper.deleteWarehouseRelations(id);
        return this.removeById(id);
    }

    public List<CategoryVO> getProductCategories(Long productId) {
        return baseMapper.getProductCategories(productId);
    }

    @Override
    public void saveProductCategory(Long productId, Long categoryId) {
        baseMapper.saveCategoryRelations(productId, Arrays.asList(categoryId));
    }

    @Override
    public void deleteProductCategory(Long productId, Long categoryId) {
        baseMapper.deleteOneCategoryRelation(productId, categoryId);
    }

    @Override
    public Page<CountryVO> getProductCountryPage(Long productId, BasePageQuery query) {
        Page<CountryVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return countryMapper.getProductCountryPage(productId, page);
    }

    @Override
    public CountryVO getProductCountry(Long productId, String countryCode) {
        return countryMapper.getProductCountry(productId, countryCode);
    }

    @Override
    public void saveProductCountry(Long productId, String countryCode) {
        countryMapper.saveProductCountry(productId, countryCode);
    }

    @Override
    public void deleteProductCountry(Long productId, String countryCode) {
        countryMapper.deleteProductCountry(productId, countryCode);
    }
}
